package com.wjk.kylin.modules.system.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.ResponseData;
import com.wjk.kylin.core.common.redis.RedisRepository;
import com.wjk.kylin.core.constant.Constants;
import com.wjk.kylin.modules.system.vo.LoginVO;
import com.wjk.kylin.core.utils.SecurityUtils;
import com.wjk.kylin.modules.system.vo.RouterVO;
import com.wjk.kylin.modules.system.menu.entity.Menu;
import com.wjk.kylin.modules.system.menu.service.IMenuService;
import com.wjk.kylin.modules.system.role.service.IRoleService;
import com.wjk.kylin.modules.system.user.entity.User;
import com.wjk.kylin.modules.system.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.*;
import java.util.List;


/**
 * @author wangjkui
 * @description 登录控制器
 * @date 2020/5/20
 */
@Slf4j
@Api("登录")
@RestController
public class LoginController extends BaseController {
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisRepository redisRepository;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleService roleService;


    /**
     * 登录
     */
    @ApiOperation("登录")
    @Log(title = "登录") /*,isSaveRequestData = false*/
    @PostMapping("/login")
    public ResponseData login(@Validated @RequestBody LoginVO login) {
        Map<String, Object> tokenMap = userService.login(
                login.getUsername(), login.getPassword(), login.getCode(), login.getUuid());

        return ResponseData.success().data(tokenMap);
    }

    /**
     * 获取用户信息
     */
    @ApiOperation("用户信息")
    @GetMapping("/getInfo")
    public ResponseData getInfo() {
        String userId = SecurityUtils.getUserId();
        //用户信息
        User user = userService.getById(userId);
        // 角色集合
        Set<String> roles = roleService.findRolePermissionByUser(user);
        // 权限集合
        Set<String> permissions = menuService.findMenuPermsByUserToUser(user);
        Map<String, Object> map = MapUtil.newHashMap(3);
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return ResponseData.success().data(map);
    }

    /**
     * 获取路由信息
     */
    @ApiOperation("路由信息")
    @GetMapping("/getRouters")
    public ResponseData getRouters() {
        String userId = SecurityUtils.getUserId();

        List<RouterVO> list = menuService.getRouters(userId);

        return ResponseData.success().data("router",list);
    }

    /**
     * 生成验证码
     */
    @ApiOperation("验证码")
    @GetMapping("/captcha")
    public ResponseData captcha() {
        //生成验证码
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(111, 36);
        captcha.setBackground(Color.gray);

        String code = captcha.getCode();
        String imageBase64 = captcha.getImageBase64();

        String uuid = IdUtil.simpleUUID();
        // 唯一标识
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        redisRepository.setExpire(verifyKey, code, Constants.CAPTCHA_EXPIRATION);

        Map<String, Object> map = MapUtil.newHashMap(2);
        map.put("uuid", uuid);
        map.put("img", imageBase64);

        return ResponseData.success().data(map);

    }

}
